<template>
  <div class='container'>
    <el-dialog :title="titleFn" :visible="showDialog" @close="closeDialog" @open="getSimpleDirectory" width="400px">
  <el-form :model="form" :rules="rules" ref="formRef">
    <el-form-item label="所属学科" prop="subjectID" v-if="!$route.query.id">
      <el-select v-model="form.subjectID" placeholder="请选择" style="width:75%">
        <el-option v-for="item in simpleList" :key="item.value" :label="item.label" :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="目录名称" prop="directoryName">
      <el-input v-model="form.directoryName" style="width:75%" placeholder="请输入目录名称"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="closeDialog">取 消</el-button>
    <el-button type="primary" @click="handleSubmit">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { simple, add, detail, update } from '@/api/hmmm/directorys.js'

export default {
  name: 'directorys-add',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        subjectID: '',
        directoryName: ''
      },
      simpleList: [],
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请选择学科', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    titleFn () {
      return this.form.id ? '修改目录' : '新增目录'
    }
  },
  methods: {
    // 关闭弹框
    closeDialog () {
      this.$emit('update:showDialog', false)
      this.$refs.formRef.resetFields()
      this.form = {
        subjectID: '',
        directoryName: ''
      }
    },
    // 获取简单目录列表
    async getSimpleDirectory () {
      const { data } = await simple()
      this.simpleList = data
    },
    // 修改弹窗显示回显
    async getDetail (id) {
      const { data } = await detail(id)
      console.log(data)
      this.form = data
    },
    // 提交新增修改请求
    handleSubmit () {
      this.$refs.formRef.validate(async boo => {
        if (!boo) return
        if (this.form.id) {
          await update(this.form)
        } else {
          await add(this.form)
        }
        this.$message.success(`${this.form.id ? '修改' : '新增'}成功`)
        this.closeDialog()
        this.$emit('add')
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
